package commonServer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import eu.pago.chat.gui.EStatus;
import eu.pago.chat.gui.InfoFriend;

public class ServerCentral extends UnicastRemoteObject implements IServer {

	private static ArrayList<String> numeFisiere = new ArrayList<String>();
	private static File file;
	protected static List<String> clientsName = new ArrayList<String>();
	protected static String nume;
	protected static String ip;
	protected static EStatus status;
	private final String name;

	protected ServerCentral(String name) throws RemoteException {
		super();
		this.name = name;
		numeFisiere = actualizeazaFisiere();
		// TODO Auto-generated constructor stub
	}

	private static ArrayList<String> actualizeazaFisiere() {
		File folder = new File(".");
		ArrayList<String> list = new ArrayList<>();

		for (File fileEntry : folder.listFiles()) {
			if (!(fileEntry.isDirectory()) && (fileEntry.toString().contains(".txt"))) {
				list.add(fileEntry.toString().substring(2).replace(".txt", ""));
			}
		}

		return list;
	}

	/**
	 * amabda
	 */
	private static final long serialVersionUID = 1L;

	// se deschide fereastra de addUser si se completeaza detaliile cu
	// precizarea ca la
	// signUp statusu' e din start AVAILABLE si lista de prieteni e null

	@Override
	public void addUser(String username, String ip) throws RemoteException {
		// TODO Auto-generated method stub
		boolean ceva = true;
		numeFisiere = actualizeazaFisiere();
		for (String s : numeFisiere) {
			if (s.equals(username))
				ceva = false;
		}
		if (ceva) {
			List<String> l = new ArrayList<String>();
			writeFile(username, ip, EStatus.AVAILABLE, l); // momentan nu avem
															// ip, dar nu
															// schimbam metoda
		}
	}

	@Override
	public List<InfoFriend> populateFriendsList(String username) throws RemoteException {
		return readFriendsInfo(username);
	}

	@Override
	public void setStatus(String username, String ip, EStatus status, List<String> friends) throws RemoteException {
		// TODO Auto-generated method stub
		writeFile(username, ip, status, friends);
	}

	// putem face asa: cineva face signIn; daca user-ul exista se apeleaza
	// metoda
	// de populateFriendsList pt acel user; daca user-ul nu exista sa genereaza
	// un
	// mesaj si acesta este rugat sa faca intai signUP(adica addUser) din meniul
	// de signIN

	@Override
	public List<InfoFriend> signIn(String username) throws RemoteException {
		// TODO Auto-generated method stub
		numeFisiere = actualizeazaFisiere();
		for (String s : numeFisiere) {
			if (s.equals(username)) {
				return populateFriendsList(s);
			}
		}
		return null;
	}

	public static void writeFile(String username, String ip, EStatus status, List<String> friends) {
		numeFisiere = actualizeazaFisiere();
		file = new File(username + ".txt");

		numeFisiere.add(username);

		if (file.exists()) {
			file.delete();
		}
		try {
			FileWriter fw = new FileWriter(file, true);
			PrintWriter bw = new PrintWriter(fw);

			bw.print(username + " ");
			bw.print(ip + " ");
			bw.println(status);

			bw.println("Friends ");
			for (String s : friends) {
				bw.println(s);
			}

			bw.close();
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void readFile(String username) {

		File f = new File(username + ".txt");
		Scanner sc = null;
		try {
			sc = new Scanner(f);
			if (sc.hasNext()) {
				nume = sc.next();
			}
			if (sc.hasNext()) {
				ip = sc.next();
			}
			if (sc.hasNext()) {
				status = EStatus.valueOf(sc.next());

			}
			if (sc.hasNext() && "Friends".equalsIgnoreCase(sc.next())) {
				sc.next();
				while (sc.hasNext()) {
					clientsName.add(sc.next());
				}
			}
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		sc.close();
	}

	public static InfoFriend readFirstLine(String username) {

		File f = new File(username + ".txt");
		Scanner sc = null;
		try {
			sc = new Scanner(f);
			if (sc.hasNext()) {
				nume = sc.next();
			}
			if (sc.hasNext()) {
				ip = sc.next();
			}
			if (sc.hasNext()) {
				status = EStatus.valueOf(sc.next());
				// status = sc.next();
			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}

		sc.close();
		InfoFriend info = new InfoFriend(nume, ip, status);
		return info;
	}

	public static List<InfoFriend> readFriendsInfo(String username) {

		ArrayList<InfoFriend> friendsList = new ArrayList<InfoFriend>();

		File f = new File(username + ".txt");
		Scanner sc = null;
		ArrayList<String> clientsName = new ArrayList<String>();
		try {
			sc = new Scanner(f);
			if (sc.hasNext()) {
				nume = sc.next();
			}
			if (sc.hasNext()) {
				ip = sc.next();
			}
			if (sc.hasNext()) {
				status = EStatus.valueOf(sc.next());
			}
			if (sc.hasNext() && "Friends".equalsIgnoreCase(sc.next())) {
				while (sc.hasNext()) {
					clientsName.add(sc.next());
				}
			}

			for (String s : clientsName) {
				friendsList.add(readFirstLine(s));
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		sc.close();
		return friendsList;
	}

	public static void main(String[] args) {
		List<InfoFriend> list = new ArrayList<>();
		ServerCentral server;
		try {
			server = new ServerCentral("alin");
			list = server.signIn("hello");

			for (InfoFriend infoFriend : list) {
				System.out.println(infoFriend.getNume());
			}
		} catch (RemoteException exc) {
			// TODO Auto-generated catch block
			exc.printStackTrace();
		}

	}

}
